UIMonthCard = Class("UIMonthCard", UIComponentBase)

function UIMonthCard:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent

    self.backGround = self:GetChild("bg")
    self.carList = self:SetDynamicList(self:GetChild("list"),self.updateItem)
end

function UIMonthCard:Refresh()
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.monthCardData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    --self.carList:RemoveChildrenToPool();
    if #self.severData.items <= 3 then
        self.carList.scrollPane.touchEffect = false;
    else
        self.carList.scrollPane.touchEffect = true;
    end
    self.carList.numItems = #self.severData.items;
    HelperFunc.ListTranstionPlay(self.carList);
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
end

function UIMonthCard:updateItem(index, obj)
    local severData = self.severData.items[index];
    local tableData = self.monthCardData[tostring(severData.subId)];
    if tableData == nil then
        obj.visible = false;
        return ;
    end
    obj.visible = true;
    local bg = obj:GetChild("bg"):GetChild("icon");
    --local card = obj:GetChild("card");
    --local title = obj:GetChild("title");
    local time = obj:GetChild("time");
    local buy = obj:GetChild("buy");
    --local frist = obj:GetChild("frist");
    --local second = obj:GetChild("second");
    --local third = obj:GetChild("third");
    --local Tips = obj:GetChild("Tips");
    --local Tips01 = obj:GetChild("Tips01");
    local Tips02 = obj:GetChild("Tips02");
    local Tips03 = obj:GetChild("Tips03");
    local Tips05 = obj:GetChild("Tips05");
    local itemList = obj:GetChild("itemList");
    local TipsDouble = obj:GetChild("TipsDouble");
    local BuffTips = obj:GetChild("BuffTips");
    local itemListBug = obj:GetChild("itemListBug");
    local typeController = time:GetController("type");
    local lockController = obj:GetController("lock");
    bg.url = "ui://UIMonthCard/"..tableData.f_BgPic;
    --card.url = "ui://UIMonthCard/"..tableData.f_CardPic;
    --title.url = "ui://UIMonthCard/"..tableData.f_TitlePic;
    Tips02.text = getLanguage(tableData.f_MonthlyCardName)
    Tips05.text = getLanguage(tableData.f_Tips05)
    buy.visible = true;
    if tableData.f_Num > 1 then
        TipsDouble.text = string.format(getLanguage("MonthlyCardDoubleRewardTips"), tableData.f_Num);
    else
        TipsDouble.text = "";
    end
    lockController.selectedIndex = 0;
    if tonumber(severData.remainDay) <= 0 then
        typeController.selectedIndex = 1;
    else
        typeController.selectedIndex = 0;
        if tableData.f_RepeatBuy == 0 then
            time:GetChild("title").text = getLanguage("MonthlyCardTips02");
            lockController.selectedIndex = 1;
            buy.visible = false;
        else
            time:GetChild("title").text = string.format(getLanguage("MonthlyCardTips03"), severData.remainDay);
        end
    end
    buy.title = "￥"..tableData.f_Price;
    buy.onClick:Clear();
    buy.onClick:Add(function() showUI(PanelNames.UIMonthCardConfirmTips, nil, self.activeId, severData.subId); end);
    --local color = decodeJsonStrToTabel(tableData.f_OnlineColor);
    --if #color > 0 then
    --    Tips01.strokeColor = Color.New(color[1] / 255, color[2] / 255, color[3] / 255);
    --else
    --    Tips01.strokeColor = Color.New(255, 255, 255);
    --end
    --Tips02.strokeColor = Tips01.strokeColor;
    --Tips03.strokeColor = Tips01.strokeColor;
    --Tips.strokeColor = Tips01.strokeColor;
    --Tips.text = getLanguage(tableData.f_McRewardTwo);
    logicMgr.CostManager.RefreshItemList(itemList, decodeJsonStrToTabel(tableData.f_DailyReward));
    logicMgr.CostManager.RefreshItemList(itemListBug, decodeJsonStrToTabel(tableData.f_TradeReward), 100);
    --self:setNewMonthCardItemReward(Tips01, tableData.f_McRewardOne, tableData.f_TradeReward);
    --self:setNewMonthCardItemReward(Tips02, tableData.f_McRewardThree, tableData.f_AllReward);
    Tips03.text = getLanguage("ProductDesc101015");
    --self:setMonthCardItemReward(third, tableData.f_McRewardThree, tableData.f_AllReward);
    --self:setMonthCardItemReward(frist, tableData.f_McRewardOne, tableData.f_TradeReward);
    --self:setMonthCardItemReward(second, tableData.f_McRewardTwo, tableData.f_DailyReward);
    BuffTips:RemoveChildrenToPool();
    if tableData.f_NobleBuff ~= "" then
        local tipsCfg = decodeJsonStrToTabel(tableData.f_NobleBuff);
        for k, v in pairs(tipsCfg) do
            local itemObj = BuffTips:AddItemFromPool();
            local buff = G.dataTable["t_ecobuff"][tostring(v)];
            if buff ~= nil then
                itemObj:GetChild("TipsBuff").text = getLanguage(buff.f_BuffDesc);
            else
                itemObj:GetChild("TipsBuff").text = "";
            end
        end
    end
end

function UIMonthCard:setNewMonthCardItemReward(obj, name, reward)
    obj.text = getLanguage(name);
    local data = decodeJsonStrToTabel(reward);
    if reward == "" then
        obj.visible = false;
    else
        obj.visible = true;
        --for k, v in pairs(data) do
            --obj.text = obj.text .. " [color=#FFA842]" .. logicMgr.ItemManager.GetItemName(v[1])..v[2].."[/color]";
        --end
        obj.text = data[1][2];
    end
end

function UIMonthCard:setMonthCardItemReward(obj, name, reward)
    local title = obj:GetChild("title");
    local list = obj:GetChild("list");
    if name == "" then
        title.visible = false;
    else
        title.visible = true;
        title.text = getLanguage(name);
    end
    if reward == "" then
        obj.visible = false;
    else
        obj.visible = true;
        local data = decodeJsonStrToTabel(reward);
        list:RemoveChildrenToPool();
        for k, v in pairs(data) do
            local item = list:AddItemFromPool()
            local itemData = logicMgr.ItemManager.getItemConstDatabyModelId(v[1]);
            if itemData ~= nil then
                item:GetChild("icon").url = EnumConst.AtlasPath.ItemAtlas..itemData.icon;
            end
            item:GetChild("num").text = ""..v[2];
        end
    end
end

function UIMonthCard:onBuySuccess(id, index)
end